package com.ycb.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ycb.common.Result;
import com.ycb.pojo.User;
import com.ycb.service.IUserService;
import com.ycb.utils.MailUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * <p>
 * 用户信息 前端控制器
 * </p>
 *
 * @author ycb
 * @since 2024-09-30
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private IUserService userService;

    @PostMapping("/sendMsg")
    public Result<String> sendMsg(@RequestBody User user, HttpServletRequest request) {
        if (user == null) {
            return Result.error("当前传入user信息失败");
        }
        //获取传入的手机
        String phone = user.getPhone();
        if (StringUtils.isEmpty(phone)) {
            return Result.error("请先填写邮箱地址");
        }
        String code = MailUtils.achieveCode();
        log.info("发送信息:{},验证码:{}",phone,code);
        try {
            MailUtils.sendTestMail(phone,code);
            request.getSession().setAttribute(phone,code);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // log.info(user.toString());
        return Result.success("发送验证码成功，请查看邮箱");
    }

    @PostMapping("/login")
    public Result<User> login(@RequestBody Map map, HttpServletRequest request) {
        //判断user里面的phone是否存在
        String phone = (String) map.get("phone");
        String code = (String) map.get("code");
        if (StringUtils.isEmpty(phone)) {
            return Result.error("请填写邮箱");
        }
        if (StringUtils.isEmpty(code)) {
            return Result.error("请填写验证码");
        }

        // //查询与session中的code是否一致
        // String codeInSession = (String)request.getSession().getAttribute(phone);
        // if (StringUtils.isEmpty(codeInSession)) {
        //     return Result.error("请重新获取验证码");
        // }
        // if (!codeInSession.equals(code)) {
        //     return Result.error("验证码输入错误，请重新输入");
        // }

        //判断是否存在该邮箱，若存在，则不保存
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhone, phone);
        User user = userService.getOne(queryWrapper);
        if (user == null) {
            user = new User();
            user.setPhone(phone);
            user.setStatus(1);
            userService.save(user);
        }
        request.getSession().setAttribute("employee", user.getId());
        return Result.success(user);
    }

    @PostMapping("/loginout")
    public Result<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("user");
        return Result.success("退出成功");
    }
}
